package com.example.he.p0311_checkbox;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.Toast;

public class CheckBoxActivity extends AppCompatActivity {
    // 声明复选按钮 ①
    private CheckBox footballChx;
    private CheckBox basketballChx;
    private CheckBox volleyballChx;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_check_box);
        // 通过findViewById获得CheckBox对象 ②
        footballChx = (CheckBox) findViewById(R.id.footballChx);
        basketballChx = (CheckBox) findViewById(R.id.basketballChx);
        volleyballChx = (CheckBox) findViewById(R.id.volleyballChx);
        //注册事件监听器 ③
        footballChx.setOnCheckedChangeListener(listener);
        basketballChx.setOnCheckedChangeListener(listener);
        volleyballChx.setOnCheckedChangeListener(listener);
    }


    private OnCheckedChangeListener listener = new OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            switch (buttonView.getId()){
                case R.id.footballChx:
                    if(isChecked){
                        Toast.makeText(CheckBoxActivity.this, "你喜欢足球", Toast.LENGTH_SHORT).show();
                    }else{
                        Toast.makeText(CheckBoxActivity.this, "你放弃了喜欢足球", Toast.LENGTH_SHORT).show();
                    }
                    break;
                case R.id.basketballChx:
                    if(isChecked){
                        Toast.makeText(CheckBoxActivity.this, "你喜欢篮球", Toast.LENGTH_SHORT).show();
                    }else{
                        Toast.makeText(CheckBoxActivity.this, "你放弃了喜欢篮球", Toast.LENGTH_SHORT).show();
                    }
                    break;
                case R.id.volleyballChx:
                    if(isChecked){
                        Toast.makeText(CheckBoxActivity.this, "你喜欢排球", Toast.LENGTH_SHORT).show();
                    }else{
                        Toast.makeText(CheckBoxActivity.this, "你放弃了喜欢排球", Toast.LENGTH_SHORT).show();
                    }
                    break;
            }
        }
    };
}